// oauth2_client: 用户名密码模式
import 'package:dio/dio.dart';
import 'package:crypto/crypto.dart';
import 'dart:convert';

import 'package:fusion_one/dtos/token.dart'; // for the utf8.encode method

Future<Token> getToken(
    {required String username, required String password}) async {
  var md5Password = md5.convert(utf8.encode(password)).toString();
  var dio = Dio();
  var headers = {'Content-Type': 'application/x-www-form-urlencoded'};
  var response = await dio.post('https://gw.fusionalign.com/connect/token',
      data: {
        'client_id': 'fusionOne.dev',
        'client_secret': '',
        'password': md5Password,
        'username': username,
        'grant_type': 'password'
      },
      options: Options(headers: headers));
  return Token.fromJson(response.data);
}
